package com.liberation.aidiancan.utils;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/12 10:52
 */

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TimeFormatUtils {
	/**
	 * :分割时间
	 */
	public static String[] splitTime(String s){
		String[] times=s.split(":");
		return times;
	}

	public static String splitTime2(String s){
		String time=s.substring(s.length()-2,s.length());
		return time;
	}

	/**
	 * @param min
	 * @return day hour min
	 */
	public static String minConvertDayHourMin(Double min) {
		String html = "0分";
		if (min != null) {
			Double m = (Double) min;
			String format;
			Object[] array;
			Integer days = (int) (m / (60 * 24));
			Integer hours = (int) (m / (60) - days * 24);
			Integer minutes = (int) (m - hours * 60 - days * 24 * 60);
			if (days > 0) {
				if (hours == 0) {
					if (minutes == 0) {
						format = "%1$,d天";
						array = new Object[]{days};
					} else {
						format = "%1$,d天%2$,d分";
						array = new Object[]{days, minutes};
					}
				} else if (minutes == 0) {
					format = "%1$,d天%2$,d时";
					array = new Object[]{days, hours};
				} else {
					format = "%1$,d天%2$,d时%3$,d分";
					array = new Object[]{days, hours, minutes};
				}

			} else if (hours > 0) {
				if (minutes == 0) {
					format = "%1$,d时";
					array = new Object[]{hours};
				} else {
					format = "%1$,d时%2$,d分";
					array = new Object[]{hours, minutes};
				}

			} else {
				format = "%1$,d分";
				array = new Object[]{minutes};
			}
			html = String.format(format, array);
		}

		return html;
	}

	/**
	 * @param min
	 * @return day hour min
	 */
	public static String minConvertDay(Double min) {
		double hour = 60.00;
		DecimalFormat d = new DecimalFormat("###,###,###.00");
		return d.format(min / hour);
	}

	/**
	 * @param min
	 * @return day hour min
	 */
	public static String minConvertHour(Double min) {
		double hour = 60.00;
		DecimalFormat d = new DecimalFormat("###,###,#0.00");
		return d.format(min / hour);
	}


	/**
	 * @param day
	 * @param hour
	 * @param min
	 * @return min
	 */
	public static int dayHourMinConvertMin(int day, int hour, int min) {
		int days = day * 24 * 60;
		int hours = hour * 60;
		return days + hours + min;
	}

	/**
	 * @param currentTime
	 * @return
	 */
	public static List<String[]> get7DaysLater(long currentTime) {
		List<String[]> list = new ArrayList<>();
		String startDay = String.format("%tF %<tT", currentTime);
		String years[] = new String[7];
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String[] result = new String[7];
		try {
			Date sd = sdf.parse(startDay);
			Calendar c = Calendar.getInstance();
			c.setTime(sd);
			String temp = sdf.format(c.getTime());
			years[0] = getYears(temp)[0];
			result[0] = getYears(temp)[1];

			for (int i = 1; i < result.length; i++) {
				c.add(Calendar.DAY_OF_MONTH, 1);
				String adds = sdf.format(c.getTime());
				years[i] = getYears(adds)[0];
				result[i] = getYears(adds)[1];
			}
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (Exception x) {
			x.printStackTrace();
		}
		list.add(result);
		list.add(years);
		return list;
	}

	private static String[] getYears(String str) {
		String[] result = new String[2];
		try {
			String[] temp = str.trim().split("-");
			result[0] = temp[0];
			result[1] = temp[1] + "-" + temp[2];
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 把“20150703145500转ms”
	 *
	 * @param time
	 * @return
	 */
	public static long getMillionSeconds(String time) {
		long millionSeconds = 0;

		if(!StringUtils.isEmpty(time)){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			try {
				millionSeconds = sdf.parse(time).getTime();
			} catch (ParseException e) {
				e.printStackTrace();
			}catch (NullPointerException e){
				e.printStackTrace();
			}
		}

		return millionSeconds;

	}
/*    */

	/**
	 * 把“20150703145500转ms”
	 *
	 * @param time
	 * @return
	 *//*
    public static long getMillionSeconds2(String time){
        long millionSeconds = 0;
        Calendar c =Calendar.getInstance();
        try {
            c.setTime(new SimpleDateFormat("yyyyMMddHH"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return millionSeconds;

    }*/

	//刚刚
	public static String converToSimpleStrDate(String time) {
		long mills = getMillionSeconds(time);
		long current = System.currentTimeMillis();
		SimpleDateFormat simpleDateFormat;
		long offSet = (current - mills) / 1000;
		java.util.Date dt = new Date(mills);
		String returnData;
		if (offSet < 5 * 60) {
			returnData = "刚刚";
		} else if (offSet >= 5 * 60 && offSet < 60 * 60) {
			returnData = offSet / 60 + "分钟前";
		} else if (offSet >= 60 * 60 && offSet < 60 * 60 * 24) {
			returnData = (offSet / 60 / 60) + "小时前";
		} else if (offSet >= 60 * 60 * 24 && offSet < 60 * 60 * 24 * 2) {
			returnData = "1天前";
		} else /*if (offSet >= 60 * 60 * 24 && offSet < 60 * 60 * 24 * 30) */ {
			//前天 -一个月前

			simpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");
			simpleDateFormat.format(dt);
			returnData = offSet / (60 * 60 * 24) + "天前";
		}
       /* } else if (offSet >= 60 * 60 * 24 * 30 && offSet < (60 * 60 * 24 * 30 * 365)) {
            //31天前
            simpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");
            returnData = simpleDateFormat.format(dt);
        } else {
            //一年前
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            returnData = simpleDateFormat.format(dt);
        }*/

		return returnData;
	}

	public static String converToMinAndSeconds(Long time){                     //将歌曲的时间转换为分秒的制度
		String min = time / (1000 * 60) + "";
		String sec = time % (1000 * 60) + "";

		if(min.length() < 2)
			min = "0" + min;
		switch (sec.length()){
			case 4:
				sec = "0" + sec;
				break;
			case 3:
				sec = "00" + sec;
				break;
			case 2:
				sec = "000" + sec;
				break;
			case 1:
				sec = "0000" + sec;
				break;
		}
		return min + ":" + sec.trim().substring(0,2);
	}


}

